package com.eqdd.yiqidian.ui.mine.pizhun;

import android.databinding.DataBindingUtil;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.UnderlineSpan;
import android.view.View;

import com.eqdd.yiqidian.AnnouncementActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.NoticesBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.TimeUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.Calendar;


/**
 * @author 吕志豪 .
 * @Date 18-4-16  上午10:39.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 * 公告/通知/通告 详情界面
 */

public class NewNoticeInfoActivity extends BaseActivity {

    private AnnouncementActivityCustom dataBinding;
    private NoticesBean item;
    private int id = -1;
    /**
     * 1-公告;2-通知;3-通告
     */
    private int who;
    private String notice;


    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_announcement);
        id = getIntent().getIntExtra("noticeId", -1);
        who = getIntent().getIntExtra(Config.WHO, -1);
        if (who == 1) {
            notice = "公告";
        } else if (who == 2) {
            notice = "通知";
        } else if (who == 3) {
            notice = "通告";
        } else {
            ToastUtil.showShort("获取详情失败");
            finish();
            return;
        }

        initTopTitleBar(View.VISIBLE, notice);
    }

    @Override
    public void initData() {

        if (id == -1) {
            ToastUtil.showShort("获取详情失败");
            finish();
            return;
        }
        OkGo.<HttpResult<NoticesBean>>post(HttpConfig.BASE_URL + HttpConfig.NOTICE_INFO)
                .params("noticeId", id)
                .execute(new DialogCallBack<HttpResult<NoticesBean>>(this) {
                    @Override
                    public void onSuccess(Response<HttpResult<NoticesBean>> response) {
                        HttpResult<NoticesBean> httpResult = response.body();
                        if (httpResult.getStatus() == 200) {
                            item = httpResult.getItems();
                            setView();
                        }
                    }
                });
    }

    @Override
    public void setView() {
        if (item != null) {
            String[] array = item.getNoticeContent().split("\n");
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < array.length; i++) {
                if (!"".equals(array[i].trim())) {
                    if (i == 0) {
                        builder.append("\t\t\t\t");
                        builder.append(array[i]);
                    } else {
                        builder.append("\n\t\t\t\t");
                        builder.append(array[i]);
                    }

                }
            }
            //下划线
            SpannableString spannableString = new SpannableString(item.getNoticeCode());
            UnderlineSpan underlineSpan = new UnderlineSpan();
            spannableString.setSpan(underlineSpan, 0, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

            Calendar calendar = Calendar.getInstance();
            String year = "[" + calendar.get(Calendar.YEAR) + "]";
            String code = item.getSimpleCompanyName() + "  " + year + spannableString + "  "
                    + item.getSimpleCompanyName() + "  签发人 : " + item.getCheckerName();
            ;


            //发布对象
            String object;
            if (item.getObjectType() == 0) {
                object = "全体员工";
            } else {
                object = item.getDepartment();
            }


            dataBinding.tvCompany.setText(item.getNoticeName());
            dataBinding.tvCode.setText(code);
            dataBinding.tvTheme.setText("《" + item.getNoticeTheme() + "》");
            dataBinding.tvPerson.setText("致 ： " + object);
            dataBinding.tvContent.setText(builder);
            dataBinding.tvInfo.setVisibility(View.GONE);
            dataBinding.tvDate.setText(TimeUtil.formatWebTime(item.getCreateTime()));
            if (item.getCheckMessage() != null && !"".equals(item.getCheckMessage())) {
                dataBinding.layoutCheckMessage.setVisibility(View.VISIBLE);
                dataBinding.tvCheckMessage.setText(item.getCheckerName() + " : " + item.getCheckMessage());
            }

            int status = getIntent().getIntExtra(Config.STATUS, -1);

            if ((who == 1 || who == 2 || who == 3) && status == 0) {
                initTopbar();
                dataBinding.llDownRoot.setVisibility(View.VISIBLE);
                dataBinding.rbReject.setOnCheckedChangeListener((buttonView, isChecked) -> {
                    if (isChecked) {
                        dataBinding.rbAgree.setChecked(false);
                    }
                });
                dataBinding.rbAgree.setOnCheckedChangeListener((buttonView, isChecked) -> {
                    if (isChecked) {
                        dataBinding.rbReject.setChecked(false);
                    }
                });
            }
        }
    }

    private void initTopbar() {

        initTopRightText("提交", v -> {
            if (dataBinding.rbReject.isChecked() && TextUtils.isEmpty(dataBinding.etMessage.getText())) {
                ToastUtil.showShort("请输入拒绝理由");
                return;
            }
            OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.NOTICE_CHECK)
                    .params("noticeId", id)
                    .params("userGuid", user.getGuid())
                    .params("message", dealText(dataBinding.etMessage.getText().toString()))
                    .params("type", dataBinding.rbAgree.isChecked() ? 1 : 2)
                    .params("notieName", notice)
                    .execute(new DialogCallBack<HttpResult>(this) {
                        @Override
                        public void onSuccess(Response<HttpResult> response) {
                            HttpResult httpResult = response.body();
                            ToastUtil.showShort(httpResult.getMsg());
                            if (httpResult.getStatus() == 200) {
                                setResult(Config.SUCCESS);
                                finish();
                            }
                        }
                    });

        });
    }

    private String dealText(String text) {
        return TextUtils.isEmpty(text) ? " " : text;
    }
}
